package com.hdss996.dictationread.activity;

import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.hdss996.dictationread.R;
import com.hdss996.dictationread.util.StatusBarUtil;
import com.hdss996.dictationread.util.SysToolUtil;

public class HomeActivity extends AppCompatActivity {
    private Button to_memo_btn;//查看说明按钮
    private Button to_mode_dication;//进入听写模式按钮
    private Button to_mode_read;//进入朗读模式按钮
    private ImageView num_config_btn;
    private ImageView toSetting;
    private ImageView home_to_like_btn;
    private ImageView home_to_share_btn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        StatusBarUtil.transparencyBar(this); //设置状态栏全透明
        StatusBarUtil.StatusBarLightMode(this); //设置白底黑字
        initView();
        initMethod();
        pms_check();
    }


    private void initView() {
        to_memo_btn = findViewById(R.id.to_memo_btn);
        to_mode_dication = findViewById(R.id.to_mode_dic);
        to_mode_read = findViewById(R.id.to_mode_read);
        toSetting = findViewById(R.id.toSetting);
        num_config_btn = findViewById(R.id.num_config_btn);
        home_to_share_btn = findViewById(R.id.home_to_share_btn);
        home_to_like_btn = findViewById(R.id.home_to_like_btn);
    }

    private void initMethod() {
        SharedPreferences app_run = getSharedPreferences("app_run", Context.MODE_PRIVATE);
        Boolean ar = app_run.getBoolean("runtime",false);
        if (!ar || ar == null) {
            SysToolUtil.setDefConfig(HomeActivity.this);
        }

        toSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(HomeActivity.this,
                        SettingActivity.class);
                startActivity(intent);

            }
        });
        to_mode_dication.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(HomeActivity.this,
                        DictationModeActivity.class);
                startActivity(intent);

            }
        });

        to_mode_read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(HomeActivity.this,
                        ReadModeActivity.class);
                startActivity(intent);

            }
        });
        num_config_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(HomeActivity.this,
                        ConfigActivity.class);
                startActivity(intent);

            }
        });
        home_to_share_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                // 如果要分享文本
                String app_name = getResources().getString(R.string.app_name);
                sendIntent.putExtra(Intent.EXTRA_TEXT, "还在为辅导孩子学习而烦恼么？\n" +
                        "“" + app_name + "”重磅出击。解放时间，助力学习！\n" +
                        "关注“生物朵爸”微信公众号，回复“听写小助手”，领取软件测试！");
                sendIntent.setType("text/plain");

                // 分享图片
                Uri imageUri = Uri.parse("android.resource://com.hdss996.dictationread/" + R.drawable.share);// 从文件系统或资源中获取图片的Uri
                sendIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
                sendIntent.setType("image/*");

                startActivity(Intent.createChooser(sendIntent,"分享听写小助手"));
            }
        });
        home_to_like_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // 定义要打开的URL链接
                String url = "https://mp.weixin.qq.com/s?__biz=MzkzMDY0MzcyMw==&mid=2247483686&idx=1&sn=19507c0d611919511fe41a304e6829df&chksm=c2766b42f501e254fe13214f997cf8a7185db559a3dd7d122a1b4db2a5121f8a9ba590f6f52c&token=1068491133&lang=zh_CN#rd";
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);
            }
        });

        //帮助按钮
        to_memo_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                SysToolUtil.showCustomAlertDialog("帮助文档","关闭",HomeActivity.this,R.layout.help_dialog_layout);
            }
        });


    }

    private boolean pms_check() {
        int writeCheck = ContextCompat.checkSelfPermission(HomeActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        int internetCheck = ContextCompat.checkSelfPermission(HomeActivity.this, Manifest.permission.INTERNET);
        if (writeCheck < 0 || internetCheck < 0) {
            AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this)
                    .setTitle("权限申请")
                    .setMessage("即将申请手机储存读写权限，该权限仅用于保存并导出音频文件。")
                    .setPositiveButton("好的", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ActivityCompat.requestPermissions(HomeActivity.this, new String[]{Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.INTERNET}, 2);
                        }
                    })
                    .setNegativeButton("拒绝", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
            AlertDialog alertDialog = builder.create();
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.show();

            return false;
        } else {
            return true;
        }
    }

}
